package org.example;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class HtmlToExcel {
    public static void main(String[] args) throws IOException {
        Editor editor = loadJson();

        // Apache POI准备工作
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Formatted Text");
        XSSFRow row = sheet.createRow(0); // 创建行

        // 创建单元格和设置样式
        XSSFRichTextString richText = new XSSFRichTextString();
        XSSFFont defaultFont = workbook.createFont();
        defaultFont.setFontHeightInPoints((short) 12);

        // 解析HTML并添加到富文本字符串
        List<Editor.ChildrenDTO> elements = editor.getChildren();
        for (Editor.ChildrenDTO element : elements) {
            if(element.isBold()){
                XSSFFont boldFont = workbook.createFont();
                boldFont.setBold(true);
                boldFont.setFontHeightInPoints((short) 12);
                richText.append(element.getText(), boldFont);

                boldFont.setColor(new XSSFColor(new Color()));
            }
            if(StrUtil.isNotBlank(element.getColor())){
                if(element.getColor().contains("rgb")){

                }
            }


            switch (element.tagName().toLowerCase()) {
                case "strong":
                    XSSFFont boldFont = workbook.createFont();
                    boldFont.setBold(true);
                    boldFont.setFontHeightInPoints((short) 12);
                    richText.append(element.text(), boldFont);
                    break;
                case "em":
                    XSSFFont italicFont = workbook.createFont();
                    italicFont.setItalic(true);
                    italicFont.setFontHeightInPoints((short) 12);
                    richText.append(element.text(), italicFont);
                    break;
                case "span":
                    XSSFFont styledFont = workbook.createFont();
                    styledFont.setFontHeightInPoints((short) 12);

                    String style = element.attr("style");
                    if (style.contains("color: red;")) {
                        styledFont.setColor(IndexedColors.RED.getIndex());
                    }
                    if (style.contains("text-decoration: underline;")) {
                        styledFont.setUnderline(Font.U_SINGLE);
                    }
                    richText.append(element.text(), styledFont);
                    break;
                default:
                    richText.append(element.text(), defaultFont);
                    break;
            }
            richText.append(" "); // 添加空格来分隔被解析的内容片段
        }

        // 将富文本写入单元格
        XSSFCell cell = row.createCell(0);
        cell.setCellValue(richText);

        // 输出到文件
        try (FileOutputStream fileOut = new FileOutputStream("HtmlToExcel.xlsx")) {
            workbook.write(fileOut);
        }

        // 关闭工作簿释放资源
        workbook.close();
    }


    @SneakyThrows
    public static Editor loadJson() {
        // 读取文件内容到字符串
        String json = new String(Files.readAllBytes(Paths.get("D:\\my-workspace\\rich-text-excel\\src\\main\\java\\org\\example\\editor.json")));
        Editor editor = JSON.parseObject(json, Editor.class);
        return editor;

    }
}